import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.ImageIcon;

public class BusBoy extends JFrame implements ActionListener{

    private Toolkit toolkit;
   //static int i = 0;
    JLabel layout;
    JButton logout =new JButton("Log Out");
    JButton clean =new JButton("Mark Clean");
    JButton[] t = new JButton[13];
    SeatingManagement seat;
    login logger;
    int st=0;
    
    public BusBoy()
    {
    	;
    }
    
    public BusBoy(SeatingManagement seats, login log) {
    	setTitle("Restaurant Master 2.0 - Busboy");
        setSize(800, 600);
        
        for(int j=0; j<13; j++)
        {
        	t[j]= new JButton(""+j);
        }
        
        seat=seats;
        logger=log;
        
        
        toolkit = getToolkit();
        Dimension size = toolkit.getScreenSize();
        setLocation((size.width - getWidth())/2, (size.height - getHeight())/2);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        JPanel panel = new JPanel();
        getContentPane().add(panel);
        
        

	panel.setLayout(null);
	
		try {
			layout = new JLabel("", new ImageIcon(new URL("http://eden.rutgers.edu/~notsonic/javasrc/layout.png")),JLabel.CENTER);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		layout.setBounds(0,0,800,400);
		
		
		
		for (int l=0;l<13;l++)
		{
			t[l].addActionListener(this);
		}
		logout.addActionListener(this);
		clean.addActionListener(this);
		
		logout.setBounds(25,525,100,30);
		clean.setBounds(500,450,150,80);
		
		t[1].setBounds(132,32,45,20);
		t[2].setBounds(197,32,45,20);
		t[3].setBounds(258,32,45,20);
		t[4].setBounds(350,25,126,35);
		t[5].setBounds(161,97,45,70);
		t[6].setBounds(161,204,45,70);
		t[7].setBounds(286,130,50,30);
		t[8].setBounds(434,130,50,30);
		t[9].setBounds(434,206,50,30);
		t[10].setBounds(114,354,50,20);
		t[11].setBounds(185,353,50,20);
		t[12].setBounds(253,353,50,20);
		
		for(int j=0; j<13; j++)
		{
			t[j].setBackground(Color.white);
		}
		
		
		for(int j=0; j<13; j++)
		{
			if (seats.getStatus(j).equals("Dirty"))
			{
				t[j].setBackground(Color.red);
			}
		}
		
		
		//add to panel
		for(int j=0; j<13; j++)
		{
			panel.add(t[j]);
		}
		
		panel.add(logout);
		panel.add(clean);
		panel.add(layout);
		
		
		
    }
    
    //event handler\
    

    
    public void actionPerformed(ActionEvent event)
    {
    	Object source = event.getSource();
    	refresh();
    	
    	if (source == logout)
    	{
    		logger.setuid(null);
    		setVisible(false);
    	}
    	
    	if (source == clean)
    	{
    		
     	   if(seat.getStatus(st).equals("Dirty"))
    		{t[st].setBackground(Color.white);
	           seat.markClean(st);
	           refresh();
    		}
    	}
    	
    	if (source== t[1])
    	{
    		st=1;
    		for (int j=0;j<13;j++)
     	   {
     		   if (t[j].getBackground()==Color.yellow)
     		   {
     			   t[j].setBackground(Color.white);
     		   }
     	   }
     	   
     	   t[1].setBackground(Color.yellow);
    	}
    	
    	if (source== t[2])
    	{
    		st=2;
    		for (int j=0;j<13;j++)
     	   {
     		   if (t[j].getBackground()==Color.yellow)
     		   {
     			   t[j].setBackground(Color.white);
     		   }
     	   }
     	   
     	   t[2].setBackground(Color.yellow);
    	}
    	
    	if (source== t[3])
    	{
    		st=3;
    		for (int j=0;j<13;j++)
     	   {
     		   if (t[j].getBackground()==Color.yellow)
     		   {
     			   t[j].setBackground(Color.white);
     		   }
     	   }
     	   
     	   t[3].setBackground(Color.yellow);
    	}
    	
    	if (source== t[4])
    	{
    		st=4;
    		for (int j=0;j<13;j++)
     	   {
     		   if (t[j].getBackground()==Color.yellow)
     		   {
     			   t[j].setBackground(Color.white);
     		   }
     	   }
     	   
     	   t[4].setBackground(Color.yellow);
    	}
    	
    	if (source== t[5])
    	{
    		st=5;
    		for (int j=0;j<13;j++)
     	   {
     		   if (t[j].getBackground()==Color.yellow)
     		   {
     			   t[j].setBackground(Color.white);
     		   }
     	   }
     	   
     	   t[5].setBackground(Color.yellow);
    	}
    	
    	if (source== t[6])
    	{
    		st=6;
    		for (int j=0;j<13;j++)
     	   {
     		   if (t[j].getBackground()==Color.yellow)
     		   {
     			   t[j].setBackground(Color.white);
     		   }
     	   }
     	   
     	   t[6].setBackground(Color.yellow);
    	}
    	
    	if (source== t[7])
    	{
    		st=7;
    		for (int j=0;j<13;j++)
     	   {
     		   if (t[j].getBackground()==Color.yellow)
     		   {
     			   t[j].setBackground(Color.white);
     		   }
     	   }
     	   
     	   t[7].setBackground(Color.yellow);
    	}
    	
    	if (source== t[8])
    	{
    		st=8;
    		for (int j=0;j<13;j++)
     	   {
     		   if (t[j].getBackground()==Color.yellow)
     		   {
     			   t[j].setBackground(Color.white);
     		   }
     	   }
     	   
     	   t[8].setBackground(Color.yellow);
    	}
    	
    	if (source== t[9])
    	{
    		st=9;
    		for (int j=0;j<13;j++)
     	   {
     		   if (t[j].getBackground()==Color.yellow)
     		   {
     			   t[j].setBackground(Color.white);
     		   }
     	   }
     	   
     	   t[9].setBackground(Color.yellow);
    	}
    	
    	if (source== t[10])
    	{
    		st=10;
    		for (int j=0;j<13;j++)
     	   {
     		   if (t[j].getBackground()==Color.yellow)
     		   {
     			   t[j].setBackground(Color.white);
     		   }
     	   }
     	   
     	   t[10].setBackground(Color.yellow);
    	}
    	
    	if (source== t[11])
    	{
    		st=11;
    		for (int j=0;j<13;j++)
     	   {
     		   if (t[j].getBackground()==Color.yellow)
     		   {
     			   t[j].setBackground(Color.white);
     		   }
     	   }
     	   
     	   t[11].setBackground(Color.yellow);
    	}
    	
    	if (source== t[12])
    	{
    		st=12;
    		for (int j=0;j<13;j++)
     	   {
     		   if (t[j].getBackground()==Color.yellow)
     		   {
     			   t[j].setBackground(Color.white);
     		   }
     	   }
     	   
     	   t[12].setBackground(Color.yellow);
    	}
    	
    	
    	
    }
    
    public void refresh()
    {
    	for(int j=0; j<13; j++)
		{
			if (seat.getStatus(j).equals("Dirty"))
			{
				t[j].setBackground(Color.red);
			}
			
			if (seat.getStatus(j).equals("Clean"))
			{
				t[j].setBackground(Color.white);
			}
			
			if (seat.getStatus(j).equals("Occupied"))
			{
				t[j].setBackground(Color.blue);
			}
		}
    }
    }

    
